package com.anyou.mall.serv.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.util.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.serv.mapper.ServConfigMapper;
import com.anyou.mall.serv.domain.ServConfig;
import com.anyou.mall.serv.service.IServConfigService;

/**
 * 商品售后配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-23
 */
@Service
public class ServConfigServiceImpl implements IServConfigService
{
    @Autowired
    private ServConfigMapper servConfigMapper;

    /**
     * 查询商品售后配置
     *
     * @param servConfigId 商品售后配置主键
     * @return 商品售后配置
     */
    @Override
    public ServConfig selectServConfigByServConfigId(Long servConfigId)
    {
        ServConfig servConfig = servConfigMapper.selectById(servConfigId);
        return servConfig;
    }

    /**
     * 查询商品售后配置列表
     *
     * @param servConfig 商品售后配置
     * @return 商品售后配置
     */
    @Override
    public List<ServConfig> selectServConfigList(ServConfig servConfig)
    {
        return servConfigMapper.selectServConfigList(servConfig);
    }

    /**
     * 新增商品售后配置
     *
     * @param servConfig 商品售后配置
     * @return 结果
     */
    @Override
    public int insertServConfig(ServConfig servConfig)
    {
        servConfig.setCreateTime(DateUtils.getNowDate());
        if(servConfig.getShopId()==null){
            servConfig.setShopId(0L);
        }
        //查询当前商家是否已存在收货配置
        ServConfig sc = new ServConfig();
        sc.setShopId(servConfig.getShopId());
        List<ServConfig> list = servConfigMapper.selectServConfigList(sc);
        AssertUtils.isTrue(list.size()>0,"当前商家已存在收货配置！");
        int insert = servConfigMapper.insert(servConfig);

        return insert;
    }

    /**
     * 修改商品售后配置
     *
     * @param servConfig 商品售后配置
     * @return 结果
     */
    @Override
    public int updateServConfig(ServConfig servConfig)
    {
        servConfig.setUpdateTime(DateUtils.getNowDate());
        int i = servConfigMapper.updateById(servConfig);
        return i;
    }

    /**
     * 批量删除商品售后配置
     *
     * @param servConfigIds 需要删除的商品售后配置主键
     * @return 结果
     */
    @Override
    public int deleteServConfigByServConfigIds(Long[] servConfigIds)
    {
        return servConfigMapper.deleteServConfigByServConfigIds(servConfigIds);
    }

    /**
     * 删除商品售后配置信息
     *
     * @param servConfigId 商品售后配置主键
     * @return 结果
     */
    @Override
    public int deleteServConfigByServConfigId(Long servConfigId)
    {
        return servConfigMapper.deleteServConfigByServConfigId(servConfigId);
    }
    /**
     * 售后配置详情
     * @return
     */
    @Override
    public ServConfig servConfigInfo() {
        ServConfig servConfig = new ServConfig();
        List<ServConfig> scList = servConfigMapper.selectList(new LambdaQueryWrapper<ServConfig>());
        if(CollUtil.isNotEmpty(scList)){
            servConfig=scList.get(0);
        }
        return servConfig;
    }

    /**
     * 新增与修改
     * @param servConfig
     */
    @Override
    public void saveOrUpdate(ServConfig servConfig) {
        if(servConfig.getServConfigId()==null){
            List<ServConfig> list = servConfigMapper.selectList(new LambdaQueryWrapper<ServConfig>());
            AssertUtils.isTrue(list.size()>0,"已存在收货配置,不能新增");
            servConfig.setCreateTime(DateUtils.getNowDate());
            servConfigMapper.insert(servConfig);
        }else{
            servConfig.setUpdateTime(DateUtils.getNowDate());
            servConfigMapper.updateById(servConfig);
        }
    }
}
